<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="trainId">
      <el-input v-model="dataForm.trainId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="startStationId">
      <el-input v-model="dataForm.startStationId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="endStationId">
      <el-input v-model="dataForm.endStationId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="距离，单位公里" prop="distance">
      <el-input v-model="dataForm.distance" placeholder="距离，单位公里"></el-input>
    </el-form-item>
    <el-form-item label="到站日期，根据这个检索车票" prop="arrivalDate">
      <el-input v-model="dataForm.arrivalDate" placeholder="到站日期，根据这个检索车票"></el-input>
    </el-form-item>
    <el-form-item label="到站时间" prop="arrivalTime">
      <el-input v-model="dataForm.arrivalTime" placeholder="到站时间"></el-input>
    </el-form-item>
    <el-form-item label="出站时间" prop="leaveTime">
      <el-input v-model="dataForm.leaveTime" placeholder="出站时间"></el-input>
    </el-form-item>
    <el-form-item label="始发站日期，可以找对应的车票" prop="departureDate">
      <el-input v-model="dataForm.departureDate" placeholder="始发站日期，可以找对应的车票"></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          trainId: '',
          startStationId: '',
          endStationId: '',
          distance: '',
          arrivalDate: '',
          arrivalTime: '',
          leaveTime: '',
          departureDate: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          trainId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          startStationId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          endStationId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          distance: [
            { required: true, message: '距离，单位公里不能为空', trigger: 'blur' }
          ],
          arrivalDate: [
            { required: true, message: '到站日期，根据这个检索车票不能为空', trigger: 'blur' }
          ],
          arrivalTime: [
            { required: true, message: '到站时间不能为空', trigger: 'blur' }
          ],
          leaveTime: [
            { required: true, message: '出站时间不能为空', trigger: 'blur' }
          ],
          departureDate: [
            { required: true, message: '始发站日期，可以找对应的车票不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/ticket/stationsection/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.trainId = data.stationSection.trainId
                this.dataForm.startStationId = data.stationSection.startStationId
                this.dataForm.endStationId = data.stationSection.endStationId
                this.dataForm.distance = data.stationSection.distance
                this.dataForm.arrivalDate = data.stationSection.arrivalDate
                this.dataForm.arrivalTime = data.stationSection.arrivalTime
                this.dataForm.leaveTime = data.stationSection.leaveTime
                this.dataForm.departureDate = data.stationSection.departureDate
                this.dataForm.createTime = data.stationSection.createTime
                this.dataForm.updateTime = data.stationSection.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ticket/stationsection/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'trainId': this.dataForm.trainId,
                'startStationId': this.dataForm.startStationId,
                'endStationId': this.dataForm.endStationId,
                'distance': this.dataForm.distance,
                'arrivalDate': this.dataForm.arrivalDate,
                'arrivalTime': this.dataForm.arrivalTime,
                'leaveTime': this.dataForm.leaveTime,
                'departureDate': this.dataForm.departureDate,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
